import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { mongoose } from '@typegoose/typegoose';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { User } from 'src/decorator/user.decorator';
import { QudaotjModel } from 'src/mongodb/models/qudaotjModel/qudaotjModel';
import { BaoMinModel } from 'src/mongodb/models/baominModel/BaoMinModel';
import { QudaobaoModel } from 'src/mongodb/models/qudaobaoModel/qudaobaoModel';
import { LanxiModel } from 'src/mongodb/models/lianxiModel/LanxiModel';
import { JobModel } from 'src/mongodb/models/jobModel/Job.models';

@Controller('web')
export class HomeController {

    constructor(
        @InjectModel(QudaotjModel) private readonly qudaotj: ModelType<QudaotjModel>,
        @InjectModel(JobModel) private readonly job: ModelType<JobModel>,
        @InjectModel(QudaobaoModel) private readonly channel: ModelType<QudaobaoModel>,
        @InjectModel(BaoMinModel) private readonly baomin: ModelType<BaoMinModel>,
        @InjectModel(LanxiModel) private readonly Contact: ModelType<LanxiModel>,
    ) { }


    @Get('qingchu')
    async qingchu() {
        this.baomin.deleteMany({}).then()
        this.channel.updateMany({}, {
            todayjihuo: 0,
            totaljihuo: 0,
            todayzhuce: 0,
            totalzhuce: 0,
            todaybaomin: 0,
            totalbaomin: 0,
            todaycopy: 0,
            totalcopy: 0,
            todaytiaozhuan: 0,
            totaltiaozhuan: 0
        }).then()

        this.Contact.updateMany({}, {
            todaynum: 0,
            totalnum: 0,
            todaycopy: 0,
            totalcopy: 0,
        }).then()

        return await this.job.updateMany({}, {
            cumulativeclick: 0,
            clicktoday: 0,
            cumulativeregistration: 0,
            signuptoday: 0
        })
    }


    /* 今日渠道统计 */
    @Get('qudaotongji')
    async qudaoTongji(@Query() query) {
        if (query.mark) {

            return await this.qudaotj.aggregate([
                {
                    /* 条件 */
                    "$match": {
                        createTime: { "$gte": Math.floor(new Date(new Date().toLocaleDateString()).getTime()) / 1000 },
                        mark: parseInt(query.mark)
                    }
                },
                {
                    "$group": {
                        "_id": "$type",
                        number: { $sum: 1 },
                    }
                }
            ])
        }
    }



    @Get('hometongji')
    @UseGuards(AuthGuard("jwtweb"))
    async hometongji(@User() user) {

        if (user.name == 'admin') {
            const channel = await this.channel.aggregate([
                {
                    $group: {
                        _id: null,
                        total_jihuo: { $sum: "$totaljihuo" },
                        today_jihuo: { $sum: "$todayjihuo" },

                        today_zhuce: { $sum: "$todayzhuce" },
                        total_zhuce: { $sum: "$totalzhuce" },

                        today_baomin: { $sum: "$todaybaomin" },
                        total_baomin: { $sum: "$totalbaomin" },

                        today_copy: { $sum: "$todaycopy" },
                        total_copy: { $sum: "$totalcopy" },

                        today_tiaozhuan: { $sum: "$todaytiaozhuan" },
                        total_tiaozhuan: { $sum: "$totaltiaozhuan" },
                    }
                }
            ])
            return { ...channel[0] }
        } else {
            const channel = await this.channel.aggregate([

                { $match: { userid: mongoose.Types.ObjectId(user._id).toHexString() } },
                {
                    $group: {
                        _id: null,
                        total_jihuo: { $sum: "$totaljihuo" },
                        today_jihuo: { $sum: "$todayjihuo" },

                        today_zhuce: { $sum: "$todayzhuce" },
                        total_zhuce: { $sum: "$totalzhuce" },

                        today_baomin: { $sum: "$todaybaomin" },
                        total_baomin: { $sum: "$totalbaomin" },

                        today_copy: { $sum: "$todaycopy" },
                        total_copy: { $sum: "$totalcopy" },

                        today_tiaozhuan: { $sum: "$todaytiaozhuan" },
                        total_tiaozhuan: { $sum: "$totaltiaozhuan" },
                    }
                }
            ])
            return { ...channel[0] }
        }
    }
}
